package se.sll.service.catalogue.common.helper;

import java.util.List;

/**
 * Helper class for extracting RIV data structures from the HSA information model.
 */
public class ExtractionHelper {

    /** Regex pattern for detecting a string with a combined Postal Code field. */
    private static final String REGEX_POSTNR_POSTORT = "^[\\d]{3}[ ]?[\\d]{2} [a-öA-Ö ]{2,}$";
    /** Regex pattern for detecting a string with a single Postal Number field. */
    private static final String REGEX_POSTNR = "^[\\d]{3}[ ]?[\\d]{2}$";
    /** Regex pattern for detecting a string with a single Postal Town field. */
    private static final String REGEX_POSTORT = "^[a-öA-Ö ]{2,}$";

    /**
     * Gets a combined RIV Postal Address String from a Hsa Postal Address List.
     * @param addressList
     * @return A combined String with the Riv Postal Address.
     */
    public static String getRivPostalAddressFromHsaPostalAddress(List<String> addressList) {
        StringBuilder postalAddress = new StringBuilder();
        if (addressList== null || addressList.size() == 0) {
            return "";
        }

        int numberOfLines = addressList.size();
        // If the last line matches the pattern for a combined Postal Code, do a simple parse.
        if (addressList.get(numberOfLines - 1).matches(REGEX_POSTNR_POSTORT)) {
            for (int x = 0; x < numberOfLines - 1; x++) {
                postalAddress.append(addressList.get(x));
                if (x != numberOfLines -2) {
                    postalAddress.append(", ");
                }

            }
        } else if (addressList.size() == 1) {
            // If the only line is not a postal code, append it to the postalAddress field.
            postalAddress.append(addressList.get(0));
        }

        // If the HSA PostalAddress list has 2 or more rows and the last two lines match the
        // split postal code fields, process accordingly.
        if (addressList.size() >= 2) {
            if (addressList.get(numberOfLines - 2).matches(REGEX_POSTNR)) {
                if (addressList.get(numberOfLines - 1).matches(REGEX_POSTORT)) {
                    for (int x = 0; x < numberOfLines - 2; x++) {
                        postalAddress.append(addressList.get(x));
                        if (x != numberOfLines - 3) {
                            postalAddress.append(", ");
                        }

                    }
                }
            }
        }
        return postalAddress.toString();
    }

    /**
     * Gets a combined RIV Postal Code (Postnummer+postadress) String from a Hsa Postal Address List.
     * @param addressList
     * @return A combined String with the Riv Postal Code.
     */
    public static String getRivPostalCodeFromHsaPostalAddress(List<String> addressList) {
        if (addressList== null || addressList.size() == 0) {
            return "";
        }

        int numberOfLines = addressList.size();

        // If the last line matches the pattern for a combined Postal Code, return it.
        if (addressList.get(numberOfLines - 1).matches(REGEX_POSTNR_POSTORT)) {
            return addressList.get(numberOfLines - 1);
        }

        // If the HSA PostalAddress list has 2 or more rows and the last two lines match the
        // split postal code fields, concatenate the rows and return them.
        if (addressList.size() >= 2) {
            if (addressList.get(numberOfLines - 2).matches(REGEX_POSTNR)) {
                if (addressList.get(numberOfLines - 1).matches(REGEX_POSTORT)) {
                    return addressList.get(numberOfLines - 2) + " " + addressList.get(numberOfLines - 1);
                }
            }
        }

        return "";
    }
}
